<?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) wangyt <wangtom365@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace App\Http\Controllers\Education;

use Illuminate\Http\Request;

use App\Http\Controllers\BaseController;
use App\Models\DictRootModel;
use App\Models\DictWordModel;
use Illuminate\Support\Facades\DB;

class ExplodeController extends BaseController
{

    public function index(Request $request)
    {
//         $action = \Route::current()->getActionName();
//         list($class, $method) = explode('@', $action);
//         $class = substr(strrchr($class,'\\'),1);
//         print_r($request->all());
//         print_r($uri = $request->path());
 
//         // With Query String...
//         $url = $request->session();
// print_r($url);
//         $tableName = 'status_original_data';
//         $tables = config('tables');
//         if (isset($tables['status'][$tableName])) {
//             $tableName = $tables['prefix'] . $tables['status'][$tableName];
//         }
//         $input = [
//             'url' => $request->path(),
//             'module' => $class,
//             'action' => $method,
//             'ip_address' => $request->ip(),
//             'updated_at' => date('Y-m-d H:i:s'),
//         ];
//         $users = DB::table($tableName)->insert($input);
        // var_dump($users);
        return view('default.tools.explodeEnglish');
    }

    public function ajax(Request $request)
    {
        // print_r($request->all());
        $content = $request->get('editContent');
        $searchStrArr = [
            '”', // 中文左引号
            '“', // 中文右引号
            '：', // 中文冒号
            '"', // 英文引号
            ':', // 英文冒号
            ',', // 英文逗号
            '，', // 中文逗号
            ';',  // 英文分号 
            '；', // 中文分号
            '_', //下划线
            '[',
            ']',
            '(',
            ')',
        ];
        $content = str_ireplace($searchStrArr, '', $content);

        $patterns = [];
        foreach ($searchStrArr as $key => $value) {
            $patterns[$key] = '/\\'.trim($value).'/';
        }
        $content = preg_replace($patterns, '', $content);

        $wordResult = [];
        $tmpData = explode(' ', $content);
        unset($content);
        foreach ($tmpData as $key => $val) {
            $val = trim($val);
            if (! isset($wordResult[$val])) {
                $wordResult[$val] = 1;
                continue;
            }
            $wordResult[$val] ++;
        }
        unset($tmpData);

        $this->renderApiSuccess('success', [
            'data' => $wordResult,
        ]);
    }

}